package com.karry.yl.service.baseinfo.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.yl.dao.baseinfo.RoomMapper;
import com.karry.yl.domain.baseinfo.Building;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.service.baseinfo.BuildingService;
import com.karry.yl.service.baseinfo.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomServiceImpl extends SuperServiceImpl<RoomMapper,Room> implements RoomService {
    @Autowired
    private BuildingService buildingService;
    @Override
    public Page<Room> selectRoomPage(Page<Room> page, Long orgId,Long buildingId, int floor) {
        return page.setRecords(baseMapper.selectRoomList(page, orgId,buildingId,floor));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveLsit(Integer[] floor, Integer[] roomNum,Long buildingId,Long orgId) {
        //循环楼层长度
        int rNum=0;
        for(int i=0;i<floor.length;i++){
            rNum+=roomNum[i];
            //当前楼层
            int f=floor[i];
            //当前楼层房间数
            int n=roomNum[i];
            int j=1;
            while(j<n+1){
                Room room = new Room();
                if(j<10){
                    room.setCode(f+"0"+j);
                }else{
                    room.setCode(f+""+j);
                }
                room.setBuildingId(buildingId);
                room.setFloor(f);
                room.setStatus(1);
                room.setOrgId(orgId);
                room.setArea(0.00);
                baseMapper.insert(room);
                j++;
            }
        }
        Building building=buildingService.selectById(buildingId);
        building.setRoomNum(rNum);
        buildingService.updateById(building);
    }

    @Override
    public List<Integer> selectRooms(Room room) {
        return  baseMapper.selectRooms(room);
    }

    @Override
    public List<Room> selectRoomsList(Room room) {
        return baseMapper.selectRoomsList(room);
    }

    @Override
    public List<Room> selectRoomListByBuildFloor(Room room) {
        return baseMapper.selectRoomListByBuildFloor(room);
    }

}
